package com.abvnet.project.water.project.controller;

import com.abvnet.common.utils.security.ShiroUtils;
import com.abvnet.framework.aspectj.lang.annotation.Log;
import com.abvnet.framework.aspectj.lang.enums.BusinessType;
import com.abvnet.framework.web.controller.BaseController;
import com.abvnet.framework.web.domain.AjaxResult;
import com.abvnet.framework.web.page.TableDataInfo;
import com.abvnet.project.water.project.domain.*;
import com.abvnet.project.water.project.service.IProjectInfoService;
import com.abvnet.project.water.project.service.IProjectLogService;
import com.abvnet.project.water.project.service.IProjectWaterService;
import com.abvnet.project.water.project.service.WordGeneratorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/project/projectSummary")
public class ProjectSummaryController extends BaseController {

    private String prefix = "project/projectSummary";

    @Autowired
    private IProjectInfoService projectInfoService;

    @Autowired
    private IProjectLogService projectLogService;

    @Autowired
    private IProjectWaterService projectWaterService;

    @RequiresPermissions("project:projectSummary:list")
    @GetMapping()
    public String projectDiagnosis()
    {
        return prefix + "/list";
    }

    /**
     * 查询项目总结列表
     */
    @RequiresPermissions("project:projectSummary:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProjectInfo projectInfo) {
        if (!ShiroUtils.getSysUser().isAdmin()) { // 非管理员，只有数据可见中的用户能看到
            projectInfo.setCreateBy(ShiroUtils.getLoginName());
            projectInfo.setVisibleUserId(String.valueOf(ShiroUtils.getUserId()));
        }
        startPage();
        List<ProjectInfo> list = projectInfoService.selectProjectInfoList(projectInfo);
        return getDataTable(list);
    }

    /**
     * 修改项目总结
     */
    @GetMapping("/edit/{projectId}")
    public String edit(@PathVariable("projectId") Long projectId, ModelMap mmap) {
//        ProjectInfo projectInfo = projectInfoService.selectProjectInfoById(projectId);
//        ProjectLog projectLog = new ProjectLog();
//        projectLog.setProjectId(projectId);
//        WaterChart waterChart = projectLogService.selectProjectLogListForData(projectLog);
//        MicroBar microBar = projectLogService.selectProjectLogMicroListForData(projectLog);
//        mmap.put("microBar", microBar);
//        mmap.put("waterChart", waterChart);
//        mmap.put("projectInfo", projectInfo);
        // 进出水，水质，日期
        ProjectInfo projectInfo = projectInfoService.selectProjectInfoById(projectId);
        ProjectLog projectLog = new ProjectLog();
        projectLog.setProjectId(projectId);
        mmap.put("projectInfo", projectInfo);
        WaterChart waterChart = projectLogService.selectProjectLogListForData(projectLog);
        MicroBar microBar = projectLogService.selectProjectLogMicroListForData(projectLog);
        ProjectWater waterStandard = projectWaterService.selectProjectWaterStandardByProjectId(projectLog.getProjectId());
        mmap.put("microBar", microBar);
        mmap.put("waterChart", waterChart);
        if (waterStandard != null) {
            mmap.put("standardCod", waterStandard.getStandardCod() == null ? -1 : waterStandard.getStandardCod());
            mmap.put("standardNh3N", waterStandard.getStandardNh3N() == null ? -1 : waterStandard.getStandardNh3N());
            mmap.put("standardTotalN", waterStandard.getStandardTotalN() == null ? -1 : waterStandard.getStandardTotalN());
            mmap.put("standardTotalP", waterStandard.getStandardTotalP() == null ? -1 : waterStandard.getStandardTotalP());
            mmap.put("standardPh", waterStandard.getStandardPh() == null ? -1 : waterStandard.getStandardPh());
            mmap.put("standardDo", waterStandard.getStandardDo() == null ? -1 : waterStandard.getStandardDo());
            mmap.put("standardT", waterStandard.getStandardT() == null ? -1 : waterStandard.getStandardT());
        } else {
            mmap.put("standardCod", -1);
            mmap.put("standardNh3N", -1);
            mmap.put("standardTotalN", -1);
            mmap.put("standardTotalP", -1);
            mmap.put("standardPh", -1);
            mmap.put("standardDo", -1);
            mmap.put("standardT", -1);
        }
        return prefix + "/edit";
    }

    /**
     * 修改保存项目总结
     */
    @RequiresPermissions("project:projectSummary:edit")
    @Log(title = "项目总结", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProjectInfo projectInfo) {
        return toAjax(projectInfoService.updateProjectInfo(projectInfo));
    }

    /**
     * 导出项目总结
     * @param projectSummary
     * @return
     */
    @RequiresPermissions("project:projectSummary:edit")
    @PostMapping("/export")
    public void export(ProjectSummary projectSummary, HttpServletRequest request, HttpServletResponse response) {
        try {
            WordGeneratorUtils.exportMillCertificateWord("summaryTemplate.ftl", request, response, projectSummary);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
